package com.turnengine.client.global.game.bean;

import com.turnengine.client.global.game.enums.GameStatus;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Game Instance.
 */
public class GameInstance implements IGameInstance, Comparable<IGameInstance> {

	/** The id. */
	private int id = -1;
	/** The name. */
	private String name = "";
	/** The game id. */
	private int gameId = -1;
	/** The version id. */
	private int versionId = -1;
	/** The host id. */
	private int hostId = -1;
	/** The status. */
	private GameStatus status = GameStatus.SHUTDOWN;

	/**
	 * Creates a new Game Instance.
	 */
	public GameInstance() {
	}

	/**
	 * Creates a new Game Instance.
	 * @param id the id
	 * @param name the name
	 * @param gameId the game id
	 * @param versionId the version id
	 * @param hostId the host id
	 * @param status the status
	 */
	public GameInstance(int id, String name, int gameId, int versionId, int hostId, GameStatus status) {
		setId(id);
		setName(name);
		setGameId(gameId);
		setVersionId(versionId);
		setHostId(hostId);
		setStatus(status);
	}

	/**
	 * Creates a new Game Instance.
	 * @param gameInstance the game instance
	 */
	public GameInstance(GameInstance gameInstance) {
		setId(gameInstance.getId());
		setName(gameInstance.getName());
		setGameId(gameInstance.getGameId());
		setVersionId(gameInstance.getVersionId());
		setHostId(gameInstance.getHostId());
		setStatus(gameInstance.getStatus());
	}

	/**
	 * Creates a new Game Instance.
	 * @param iGameInstance the i game instance
	 */
	public GameInstance(IGameInstance iGameInstance) {
		setId(iGameInstance.getId());
		setName(iGameInstance.getName());
		setGameId(iGameInstance.getGameId());
		setVersionId(iGameInstance.getVersionId());
		setHostId(iGameInstance.getHostId());
		setStatus(iGameInstance.getStatus());
	}

	/**
	 * Returns the id.
	 * @return the id.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Returns the name.
	 * @return the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Returns the game id.
	 * @return the game id.
	 */
	public int getGameId() {
		return gameId;
	}

	/**
	 * Returns the version id.
	 * @return the version id.
	 */
	public int getVersionId() {
		return versionId;
	}

	/**
	 * Returns the host id.
	 * @return the host id.
	 */
	public int getHostId() {
		return hostId;
	}

	/**
	 * Returns the status.
	 * @return the status.
	 */
	public GameStatus getStatus() {
		return status;
	}

	/**
	 * Sets the id.
	 * @param id the id to set.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Sets the name.
	 * @param name the name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Sets the game id.
	 * @param gameId the game id to set.
	 */
	public void setGameId(int gameId) {
		this.gameId = gameId;
	}

	/**
	 * Sets the version id.
	 * @param versionId the version id to set.
	 */
	public void setVersionId(int versionId) {
		this.versionId = versionId;
	}

	/**
	 * Sets the host id.
	 * @param hostId the host id to set.
	 */
	public void setHostId(int hostId) {
		this.hostId = hostId;
	}

	/**
	 * Sets the status.
	 * @param status the status to set.
	 */
	public void setStatus(GameStatus status) {
		this.status = status;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(59, 37);
		hash.append(id);
		hash.append(name);
		hash.append(gameId);
		hash.append(versionId);
		hash.append(hostId);
		hash.append(status);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof GameInstance) {
			GameInstance compare = (GameInstance) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.id, compare.id);
			equals.append(this.name, compare.name);
			equals.append(this.gameId, compare.gameId);
			equals.append(this.versionId, compare.versionId);
			equals.append(this.hostId, compare.hostId);
			equals.append(this.status, compare.status);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IGameInstance compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.id, compare.getId());
		builder.append(this.name, compare.getName());
		builder.append(this.gameId, compare.getGameId());
		builder.append(this.versionId, compare.getVersionId());
		builder.append(this.hostId, compare.getHostId());
		builder.append(this.status, compare.getStatus());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("id", id);
		if (name != null) {
			string.append("name", name);
		}
		string.append("gameId", gameId);
		string.append("versionId", versionId);
		string.append("hostId", hostId);
		if (status != null) {
			string.append("status", status);
		}
		return string.toString();
	}
}
